<?php

namespace app\impl;

use app\interfaces\Task;
use app\common\model\PropagandaEducationHistory as PehModel;
use app\common\model\PushMessage as PmModel;

class SendPropagandaEducation implements Task
{

    public function run(array $data,string $taskId): ?bool
    {
        \think\facade\Log::write("宣教队列入口:".json_encode(['batchNo' => $data['batchNo']]),'ed');
        try {
            $ids = (new PehModel())->where('batch_no', $data['batchNo'])->column('id');

            $status = [];
            foreach ($ids as $v) {
                $status[] = [
                  'id' => $v,
                  'status' => 1,
                  'job_id' => $taskId
                ];
            }
            \think\facade\Log::write("宣教队列处理数据:".json_encode($status),'ed');
            (new PehModel())->saveAll($status);

            foreach ($data['message'] as $key => $val) {
                $data['message'][$key]['job_id'] = $taskId;
            }
            (new PmModel())->saveAll($data['message']);
        } catch (\Exception $exception) {
            \think\facade\Log::write("推送宣教异常:" . $exception->getMessage(), "error");
        }
        return true;
    }
}